package com.chaowen.yixin;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.chaowen.config.Config;
import com.chaowen.util.MyHandler;
import com.chaowen.util.PreferenceHelper;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity implements OnClickListener {
	protected SlidingMenu mSlidingMenu;
	private ImageButton ivTitleBtnLeft;
	private ImageButton ivTitleBtnRigh;
	private TextView ivTitleName;
	private Fragment mContent;
	private MyHandler handler = new MyHandler(MainActivity.this);
	private long exitTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initSlidingMenu();
		initView();
	}

	//初始化View
	private void initView() {
		ivTitleBtnLeft = (ImageButton) findViewById(R.id.ivTitleBtnLeft);
		ivTitleBtnRigh = (ImageButton) findViewById(R.id.ivTitleBtnRigh);
		ivTitleName = (TextView)findViewById(R.id.ivTitleName);
		ivTitleBtnLeft.setOnClickListener(this);
		ivTitleBtnRigh.setOnClickListener(this);
		ivTitleName.setText(Config.titleName);
	}

	//初始化滑动菜单
	private void initSlidingMenu() {
		mContent = new Fragment_login(handler);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, mContent).commit();
		setBehindContentView(R.layout.main_left_layout);
		Fragment leftFragment = new LeftSlidingMenuFragment(handler);
		
		FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.main_left_fragment, leftFragment).commit();
		// customize the SlidingMenu
		mSlidingMenu = getSlidingMenu();
		mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);// 设置是左滑还是右滑，还是左右都可以滑，我这里只做了左滑
		mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// 设置菜单宽度
		mSlidingMenu.setFadeDegree(0.35f);// 设置淡入淡出的比例
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);// 设置手势模式
		mSlidingMenu.setShadowDrawable(R.drawable.shadow);// 设置左菜单阴影图片
		mSlidingMenu.setFadeEnabled(true);// 设置滑动时菜单的是否淡入淡出
		mSlidingMenu.setBehindScrollScale(0.333f);// 设置滑动时拖拽效果

		mSlidingMenu.setSecondaryMenu(R.layout.main_right_layout);
		Fragment rightFragment = new RightSlidingMenuFragment(handler);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_right_fragment, rightFragment).commit();
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivTitleBtnLeft:
			mSlidingMenu.showMenu(true);
			break;
		case R.id.ivTitleBtnRigh:
			mSlidingMenu.showSecondaryMenu();
			break;
		default:
			break;
		}

	}

	/**
	 * 菜单点击切换首页的内容
	 */
	public void switchContent(Fragment fragment) {
		mContent = fragment;
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, fragment).commit();
		getSlidingMenu().showContent();
		initView();
	}

//	@Override
//	protected void onDestroy() {
//		super.onDestroy();
//		new PreferenceHelper(MainActivity.this).saveLogin(false);
//	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exits();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 退出应用程序
	 */
	private void exits() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			exitTime = System.currentTimeMillis();
			Toast.makeText(MainActivity.this, "再按一次返回键，退出程序",
					Toast.LENGTH_SHORT).show();
		} else {
			finish();
			System.exit(0);
		}
	}
}
